<template>
    <div>
        <div class="PermissionManager">
            <el-input placeholder="请输入角色英文名" v-model="role.name">
                <template slot="prepend">ROLE_</template>
            </el-input>
            <el-input placeholder="请输入角色中文名"
                      v-model="role.nameZh"
                      @keydown.enter.native="addRoles"/>
            <el-button type="primary" size="small" icon="el-icon-plus" @click="addRoles">添加角色</el-button>
        </div>
        <div class="collapseStyle">
            <el-collapse v-model="activeName" accordion style="width: 700px"
                         @change="change">
                <el-collapse-item :title="role.nameZh" :name="role.id" v-for="(role,index) in roles"
                                  :key="index">
                    <el-card class="box-card">
                        <div slot="header" style="display: flex;justify-content: space-between">
                            <span>可访问资源</span>
                            <el-button style="float: right; padding: 3px 0;color: darkred"
                                       icon="el-icon-delete"
                                       @click="deleteRole(role)"
                                       type="text">删除按钮
                            </el-button>
                        </div>
                        <div>
                            <!-- default-checked-keys 展示已经被选中的项-->
                            <el-tree
                                    show-checkbox
                                    node-key="id"
                                    ref="tree"
                                    :key="index"
                                    :default-checked-keys="selectedMenus"
                                    :data="allMenus" :props="defaultProps"></el-tree>
                            <div style="display: flex;justify-content: flex-end">
                                <el-button size="mini" @click="cancelUpdate">取消修改</el-button>
                                <el-button type="primary" size="mini" @click="doUpdate(role.id,index)">确认修改</el-button>
                            </div>
                        </div>
                    </el-card>
                </el-collapse-item>
            </el-collapse>
        </div>
    </div>
</template>

<script>
    export default {
        name: "PermissionManager",
        data() {
            return {
                activeName: -1,
                role: {
                    name: '',
                    nameZh: ''
                },
                selectedMenus: [],
                allMenus: [],
                roles: [],
                defaultProps: {
                    children: 'children',
                    label: 'name' //展示出来的文本
                }
            }
        },
        mounted() {
            this.initRoles();
        },
        methods: {
            deleteRole(role) {
                this.$confirm('此操作将永久删除【' + role.nameZh + '】角色, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/system/basic/permiss/role/" + role.id).then(resp => {
                        if (resp) {
                            this.initRoles();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            addRoles() {
                if (this.role.name && this.role.nameZh) {
                    this.postRequest("/system/basic/permiss/role", this.role).then(resp => {
                        if (resp) {
                            this.role.name = '';
                            this.role.nameZh = '';
                            this.initRoles();
                        }
                    })
                } else {
                    this.$message.error('数据不可以为空');
                }
            },
            cancelUpdate() {
                this.activeName = -1;
            },
            doUpdate(data, index) {
                //console.log(data, index);
                let tree = this.$refs.tree[index];//通过数组下标获取当前展开树的数据
                //console.log(tree);
                let SelectedKeys = tree.getCheckedKeys(true);
                console.log(SelectedKeys);
                let url = '/system/basic/permiss/?rid=' + data;
                SelectedKeys.forEach(key => {
                    url += '&mids=' + key;
                })
                this.putRequest(url).then(resp => {
                    if (resp) {
                        this.activeName = -1;
                    }
                })
            },
            change(rid) {
                if (rid) {
                    this.initAllMenus();
                    this.initSelectedMenus(rid);
                }
            },
            initSelectedMenus(rid) {
                this.getRequest("/system/basic/permiss/mids/" + rid).then(resp => {
                    this.selectedMenus = resp;
                })
            },
            initAllMenus() {
                this.getRequest("/system/basic/permiss/menus").then(resp => {
                    if (resp) {
                        this.allMenus = resp;
                    }
                })
            },
            initRoles() {
                this.getRequest("/system/basic/permiss/").then(resp => {
                    if (resp) {
                        this.roles = resp;
                    }
                })
            }
        }
    }
</script>

<style scoped>
    .PermissionManager {
        display: flex;
        justify-content: flex-start;
    }

    .PermissionManager .el-input {
        width: 300px;
        margin-right: 6px;
    }

    .collapseStyle {
        display: flex;
        justify-content: flex-start;
        margin-top: 10px;
        width: 700px;
    }
</style>